package com.kr.weitao.util.rest.condition;

import com.kr.weitao.exception.CommonException;
import com.kr.weitao.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/** Created by paco on 5/6/2017. */
public enum ELogic {
  and("AND", "并且"),
  or("OR", "或者");
  //exists("exists","包含"),
  //notexists("not exists","不包含");

  private static Map<String, ELogic> alllogic = new HashMap<String, ELogic>();

  static {
    alllogic.put("and", and);
    alllogic.put("or", or);
    //alllogic.put("exists",exists);
    //alllogic.put("notexists",notexists);
  }

  private String code;
  private String desc;

  ELogic(String code, String desc) {
    this.code = code;
    this.desc = desc;
  }

  public static ELogic getLogic(String code) {
    ELogic el = null;
    if (StringUtils.isNull(code)) {
      throw new CommonException("-30", "逻辑运算符不能为空");
    }
    String tempCode = code.replaceAll("\\s+", "");
    if (!alllogic.containsKey(tempCode)) {
      throw new CommonException("-30", "逻辑运算符错误");
    }
    el = alllogic.get(tempCode.toLowerCase());

    return el;
  }

  public String getCode() {
    return this.code;
  }

  public String getDesc() {
    return this.desc;
  }
}
